package com.chen.module.testproducerconsumer.test;

import com.chen.module.testproducerconsumer.jdasynctool.callback.ICallback;
import com.chen.module.testproducerconsumer.jdasynctool.callback.IWorker;
import com.chen.module.testproducerconsumer.jdasynctool.executor.timer.SystemClock;
import com.chen.module.testproducerconsumer.jdasynctool.worker.WorkResult;

import java.text.SimpleDateFormat;

/**
 * create by chen on 2020/4/2 14:34
 * Email:chenshaopeng90@hotmail.com
 */
public class ParWorker3 implements IWorker<String, String>, ICallback<String, String> {

    @Override
    public String action(String object) {
        try {
            Thread.sleep(30_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(SystemClock.now());
        return "ParWorker3 result = " + dateNowStr + "---param = " + object + " from 3";
    }

    @Override
    public String defaultValue() {
        return "worker3--default";
    }

    @Override
    public void begin() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(SystemClock.now());
        System.out.println("ParWorker3" + " start --" + dateNowStr);
    }

    @Override
    public void result(boolean success, String param, WorkResult<String> workResult) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(SystemClock.now());
        if (success) {
            System.out.println("ParWorker3 callback worker3 success--" + dateNowStr + "----" + workResult.getResult()
                    );
        } else {
            System.err.println("callback worker3 failure--" + dateNowStr + "----"  + workResult.getResult()
                   );
        }
    }

}